/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sequence.ui.window;

import java.awt.Dimension;
import java.awt.Font;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;

import sequence.model.Phases;
import sequence.model.Sequence;
import sequence.utilities.Sex;

/**
 * The Class InfoSequencePanel.
 *
 * @author arnaud
 */
public class InfoSequencePanel extends javax.swing.JPanel {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/**
	 * Creates new form InfoSequencePanel.
	 */
	public InfoSequencePanel() {
		initComponents();
	}

	/**
	 * Instantiates a new info sequence panel.
	 *
	 * @param sequence the sequence
	 */
	public InfoSequencePanel(final Sequence sequence) {
		initComponents();
		numberActivities.setText(String.valueOf(sequence.activityNumber()));
		age.setText(String.valueOf(sequence.getPatient().getAge()));
		if (sequence.getPatient().getSex() == Sex.female)
			sex.setText("female");
		else
			sex.setText("male");
		actuator.setText(sequence.getPatient().getPosition().getPosition());
		note.setText(sequence.getPatient().getNote().getNote());
		chartPanel.add(createChart(sequence));
		chartPanel.validate();
		revalidate();
	}

	/**
	 * Creates the chart.
	 *
	 * @param sequence the sequence
	 * @return the chart panel
	 */
	private ChartPanel createChart(final Sequence sequence) {
		final Phases phases = sequence.getPhases();
		final List<Integer> phasesDuration = sequence.phaseDuration();
		final DefaultPieDataset dataset = new DefaultPieDataset();
		for (int i = 0; i < phases.size(); i++)
			dataset.setValue(phases.get(i).getName(), phasesDuration.get(i));
		final JFreeChart chart = ChartFactory.createPieChart("Phase duration", // chart
																				// title
				dataset, // data
				false, // include legend
				true, false);

		final PiePlot plot = (PiePlot) chart.getPlot();
		plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12));
		plot.setCircular(false);
		plot.setLabelGap(0.02);

		final ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new Dimension(500, 270));
		chartPanel.setMinimumSize(new Dimension(500, 270));

		return chartPanel;
	}

	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {

		generalInformationLabel = new javax.swing.JLabel();
		numberActivitiesLabel = new javax.swing.JLabel();
		patientInformationLabel = new javax.swing.JLabel();
		ageLabel = new javax.swing.JLabel();
		sexLabel = new javax.swing.JLabel();
		actuatorLabel = new javax.swing.JLabel();
		noteLabel = new javax.swing.JLabel();
		numberActivities = new javax.swing.JLabel();
		age = new javax.swing.JLabel();
		sex = new javax.swing.JLabel();
		actuator = new javax.swing.JLabel();
		note = new javax.swing.JLabel();
		chartPanel = new javax.swing.JPanel();

		generalInformationLabel.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
		generalInformationLabel.setText("General information");

		numberActivitiesLabel.setText("Number of activities :");

		patientInformationLabel.setFont(new java.awt.Font("Dialog", 1, 18)); // NOI18N
		patientInformationLabel.setText("Patient information");

		ageLabel.setText("Age :");

		sexLabel.setText("Sex :");

		actuatorLabel.setText("Actuator :");

		noteLabel.setText("Note :");

		numberActivities.setText("numberActivities");

		age.setText("age");

		sex.setText("sex");

		actuator.setText("actuator");

		note.setText("note");

		final javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
		setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		chartPanel,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		javax.swing.GroupLayout.DEFAULT_SIZE,
																		Short.MAX_VALUE)
																.addContainerGap())
												.addGroup(
														layout.createSequentialGroup()
																.addGroup(
																		layout.createParallelGroup(
																				javax.swing.GroupLayout.Alignment.LEADING)
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										numberActivitiesLabel)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																										211,
																										Short.MAX_VALUE)
																								.addComponent(
																										numberActivities))
																				.addGroup(
																						layout.createSequentialGroup()
																								.addGroup(
																										layout.createParallelGroup(
																												javax.swing.GroupLayout.Alignment.LEADING)
																												.addComponent(
																														generalInformationLabel)
																												.addComponent(
																														patientInformationLabel))
																								.addGap(0,
																										0,
																										Short.MAX_VALUE))
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										ageLabel)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										Short.MAX_VALUE)
																								.addComponent(
																										age))
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										sexLabel)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										Short.MAX_VALUE)
																								.addComponent(
																										sex))
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										actuatorLabel)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										Short.MAX_VALUE)
																								.addComponent(
																										actuator))
																				.addGroup(
																						layout.createSequentialGroup()
																								.addComponent(
																										noteLabel)
																								.addPreferredGap(
																										javax.swing.LayoutStyle.ComponentPlacement.RELATED,
																										javax.swing.GroupLayout.DEFAULT_SIZE,
																										Short.MAX_VALUE)
																								.addComponent(
																										note)))
																.addContainerGap(
																		12,
																		javax.swing.GroupLayout.PREFERRED_SIZE)))));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(generalInformationLabel)
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														numberActivitiesLabel)
												.addComponent(numberActivities))
								.addGap(18, 18, 18)
								.addComponent(patientInformationLabel)
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(ageLabel)
												.addComponent(age))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(sexLabel)
												.addComponent(sex))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(actuatorLabel)
												.addComponent(actuator))
								.addGap(18, 18, 18)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(noteLabel)
												.addComponent(note))
								.addGap(18, 18, 18)
								.addComponent(chartPanel,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										194, Short.MAX_VALUE).addContainerGap()));
	}// </editor-fold>//GEN-END:initComponents
		// Variables declaration - do not modify//GEN-BEGIN:variables

	/** The actuator. */
		private javax.swing.JLabel actuator;
	
	/** The actuator label. */
	private javax.swing.JLabel actuatorLabel;
	
	/** The age. */
	private javax.swing.JLabel age;
	
	/** The age label. */
	private javax.swing.JLabel ageLabel;
	
	/** The chart panel. */
	private javax.swing.JPanel chartPanel;
	
	/** The general information label. */
	private javax.swing.JLabel generalInformationLabel;
	
	/** The note. */
	private javax.swing.JLabel note;
	
	/** The note label. */
	private javax.swing.JLabel noteLabel;
	
	/** The number activities. */
	private javax.swing.JLabel numberActivities;
	
	/** The number activities label. */
	private javax.swing.JLabel numberActivitiesLabel;
	
	/** The patient information label. */
	private javax.swing.JLabel patientInformationLabel;
	
	/** The sex. */
	private javax.swing.JLabel sex;
	
	/** The sex label. */
	private javax.swing.JLabel sexLabel;
	// End of variables declaration//GEN-END:variables
}
